<?php
include('include/config.inc.php');
if(!isset($_SESSION['s_activId'.$cfgPack])
  || (isset($_SESSION['s_userType'.$cfgPack]) && isset($cfgFileNames['party'][$_SESSION['s_userType'.$cfgPack]]) 
      && $cfgFileNames['party'][$_SESSION['s_userType'.$cfgPack]] == 0))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{
  $count = 0;
  $productArray = array();
  
  $selectProduct = "SELECT item.itemId, item.itemName, category.categoryName,subcategory.subCategoryName,
                           item.currentStock, item.maxLevel, item.minLevel
                      FROM item
                      JOIN category ON category.categoryId = item.categoryId
                      JOIN subcategory ON subcategory.subCategoryId = item.subCategoryId
                     WHERE item.currentStock <= item.minLevel
                     ORDER BY item.currentStock ASC";
  $selectProductRes = mysql_query($selectProduct);
  while($row = mysql_fetch_array($selectProductRes))
  {
	  $selectCurrStk = "SELECT deliverychallandetail.itemId, deliverychallandetail.quantity,
	                           deliverychallandetail.deliveryChallanMasterId,
	                           deliverychallandetail.deliveryChallanDetailId,
	                           deliverychallanmaster.delivaryDate, 
	                           deliverychallanmaster.challanType,
	                           goodsreturn.goodsReturnId
	                      FROM deliverychallanmaster
	                      JOIN deliverychallandetail 
	                           ON deliverychallandetail.deliveryChallanMasterId = deliverychallanmaster.deliveryChallanMasterId
	                      LEFT JOIN goodsreturn 
	                        ON deliverychallandetail.deliveryChallanDetailId =  goodsreturn.deliveryChallanDetailId          
	                     WHERE deliverychallandetail.itemId = ".$row['itemId']."
	                     ORDER BY deliverychallanmaster.delivaryDate ASC";
	  $selectCurrStkRes = mysql_query($selectCurrStk);
	  while($row1 = mysql_fetch_array($selectCurrStkRes)) 
	  {
	  	if($row1['challanType'] = 'Delivery Challan/Note' && $row1['goodsReturnId'] != NULL)
	  	{
	  		//$productArray[$count]['currentStock']    = $row1['currentStock'];
	  	}
	  	elseif($row1['challanType'] = 'Delivery Challan/Note' && $row1['goodsReturnId'] == NULL)
	  	{
	  		//$productArray[$count]['currentStock']    = $row1['currentStock'];
	  	}
	  	elseif($row1['challanType'] = 'Goods Inward Note')
	  	{
	  		//$productArray[$count]['currentStock']    = $row1['currentStock'];
	  	}
	  	elseif($row1['challanType'] = 'Goods Rejection')
	  	{
	  		//$productArray[$count]['currentStock']    = $row1['currentStock'];
	  	}
	  } 	
	  
  	$productArray[$count]['itemName']        = $row['itemName'];
  	$productArray[$count]['categoryName']    = $row['categoryName'];
  	$productArray[$count]['subCategoryName'] = $row['subCategoryName'];
  	$productArray[$count]['maxLevel']        = $row['maxLevel'];
  	$productArray[$count]['minLevel']        = $row['minLevel'];
  	$productArray[$count]['currentStock']    = $row['currentStock'];
  	$count++;
  }
  include("./bottom.php");
  $smarty->assign("productArray",$productArray);
  $smarty->display("productRegisterRavi.tpl");
}
?>